<?php
namespace Home\Controller;
use Think\Controller;
class JubaoController extends BaseController{
	public function _initialize(){
		parent::_initialize();
		$this -> readnum = D('Readnum');
		$this -> gzhis = D('Gzhis');
		$this -> uid = session("userid");
		$this -> jubao = D("Jubao");
		$this -> relation = D("Relation");
		$this -> user = D("User");
		$this -> jubaoview = D("Jubaoview");
	}

	public function jubaohis(){
		$where['uid'] = session("userid");
		$res = $this -> jubao -> where($where) -> order('year desc,datestr desc') -> select();
		if ($res) {
			$this -> assign('mygz',$res);
		}
		$this -> display();
	}

	public function juinfo(){
		$where['id'] = $_GET['jid'];
		$res = $this -> jubao -> where($where) -> limit(1) -> select();
		if ($res) {
			$this -> assign('jubao',$res[0]);
			$picdata = M("Jpic") -> where(array('jid'=>$res[0]['id'])) -> select();
			$reldata = $this -> relation -> where(array('jid'=>$res[0]['id'])) -> select();
			$this -> assign('picsrc',$picdata);
			$this -> assign('reldata',$reldata[0]);
		}
		$this -> display();
	}

	public function mygz(){
		
		$where['uid'] = session("userid");
		$where['type'] = $_GET['type'];
		$res = $this -> gzhis -> where($where) -> order('ctime desc') -> select();
		
		if ($res) {
			$this -> assign('mygz',$res);
		}

		$this -> display();
	}
	
	public function addgz(){
		$data['name'] = $_POST['name'];
		$data['cardid'] = $_POST['idcardno'];
		$data['area'] = $_POST['area'];
		$data['snum'] = $_POST['snum'];
		$data['type'] = 2;
		$data['uid'] = session("userid");
		$data['status'] = 0;
		$data['ctime'] = date('Y-m-d',time());
		$res = $this -> gzhis -> add($data);
		if ($res) {
			apiResponse("success","关注成功！");
		}else{
			apiResponse("error","关注失败！");
		}

	}

	public function showinfo(){
		$res = json_decode(S('res'),true);
		$res1 = $res[$_GET['ind']-1];
		$this -> assign('jubao',$res1);
		if ($_GET['type'] == 'selfping') {
			if ($res1) {
				$picdata = M("Jpic") -> where(array('jid'=>$res1['id'])) -> select();
				$reldata = $this -> relation -> where(array('jid'=>$res1['id'])) -> select();
				$this -> assign('picsrc',$picdata);
				$this -> assign('reldata',$reldata[0]);
			}
			$this -> display('juinfo');
		}else{
			//dump($res1);
			$this -> display('showinfo');
		}
	}

	public function jbinfo(){
		if ($_GET['type']=="contry") {
			$resdata = S("resdata");
			$idcard = $_GET['cardid'];
			$res1 = json_decode($resdata,true);
			$res = array();
			foreach ($res1 as $key => $value) {
				if ($idcard == $value['idcard']) {
					array_push($res,$value);
				}
			}
			$name = S("findname");
			$where['name'] = $name;
			// $this -> assign("ind",$_REQUEST['ind']-1);
			$this -> assign("name",$name);
		}else{
			if ($_GET['name']) {
				$where['name'] = $_GET['name'];
			}
			if ($_GET['cardid']) {
				$where['idcardno'] = $_GET['cardid'];
				$where['idcard'] = $_GET['cardid'];
			}
			if (empty($_GET['type'])) {
				$where['status'] = 1;
			}
			if ( $_GET['type'] == 9 ) {
				$res = $this -> jubao -> where($where) -> limit(1) -> order('id desc') -> select();
			}else{
				$res = $this -> jubao -> where($where) -> select();
			}
			if ($res) {
				$search = substr($res[0]['idcardno'], 10,4);
				$strcid = str_replace($search, "****", $res[0]['idcardno']);
				$res[0]['idcardno'] = $strcid;
			}
		}
		$where['type'] = 2;
		$count = $this -> gzhis -> where($where) -> count();
		$where['uid'] = session("userid");
		$isgz = $this -> gzhis -> where($where) -> count();
		S('res',json_encode($res));
		$this -> assign("resdata",$res);
		$this -> assign("count",$count);
		$this -> assign("isgz",$isgz);
		$this -> display();
	}

	public function jubaolist(){
		$findnum = session("findnum");
		$tel = session("tel");
		if (empty($tel)) {
			if (empty($findnum)) {
				session("findnum",1);
			}else{
				if ($findnum > 4) {
					header("Content-type: text/html; charset=utf-8"); 
					echo "<span style='font-size:3rem'>免费查询次数用完,<a href='".U('Member/login')."'>点击</a>绑定手机号继续查询</span>";
					exit();
				}else{
					session("findnum",++$findnum);
				}
			}
		}
		$tel = $_GET['tel'];
		if (empty($_GET['tel'])) {
			$name = $_GET['name'];
			$type = $_GET['type'];
			$cardid = $_GET['cardid'];
			$where['keyworks'] = $name;
			$res = $this -> readnum -> where($where) -> select();
			$renum = 0;
			if ($res) {
				$res1 =$res[0];
				$renum = $res1['num']+1;
				$read = array(
					'id' => $res1['id'],
					'num' => $renum
					);
				$this -> readnum -> save($read);
			}else{
				$read = array(
					'keyworks' => $name,
					'num' => $renum,
					'status' => 0 
					);
				$this -> readnum -> add($read);
			}
			$gzwhere['ctime'] = date('Y-m-d',time());
			if ($cardid) {
				$gzwhere['cardid'] = $cardid;
			}
			$gzwhere['type'] = 1;//查询历史
			$gzres = $this -> gzhis -> where($gzwhere) -> select();
			if (empty($gzres)) {
				$gzwhere['name'] = $name;
				$gzwhere['uid'] = $this -> uid;
				$this -> gzhis -> add($gzwhere);
			}
			$this -> assign("readnum",$renum);
			if ($type == "contry") {
				$url1 = "http://api.jisuapi.com/creditblacklist/query?appkey=af2ee50db2fa0152&realname=$name";

				if ($cardid) {
					$url = "http://e.apix.cn/apixcredit/blacklist/dishonest?type=person&name=$name&cardno=$cardid";
					$res = $this -> getApi($url);
					$arrdata = json_decode($res,true);
					$url1 = $url1+"&idcard=$cardid";
				}
				$res1 = $this -> curl($url1);
				$arrdata1 = json_decode($res1,true);
				$resdata1 = $arrdata1['result']['list'];
				$cardarr = array();
				$resdata = array();
				foreach ($resdata1 as $key => $value){
					$index = array_search($value['idcard'],$cardarr);
					if ( $index == false && $index !== 0 ){
						array_push($cardarr,$value['idcard']);
						$value['count'] = 1;
						array_push($resdata,$value);
					}else{
						$resdata[$index]['count'] = $resdata[$index]['count'] + 1;
					}
				}
				$this -> assign('arrdata1',json_encode($arrdata1));
				if ( $cardid && $resdata) {
					array_merge($resdata,$arrdata['data']);
				}else if($cardid){
					$resdata = $arrdata['data'];
				}
				//dump($resdata);
				S("resdata",json_encode($resdata1));
				S("findname",$name);
				//$coun = count( $resdata );
				//$this -> assign("count",$coun);
				$this -> assign("data1",$resdata);
				if (!$resdata) {
					$this -> display("nolist");
				}else{
					$this -> display();
				}
				exit();
			}else{
				$piwhere['name'] = $name;
				$piwhere['cardid'] = $cardid;
				$piwhere['status'] = 1;
				$pires = $this -> jubao -> where($piwhere) -> select();
					$cardarr = array();
					$resdata = array();
					foreach ($pires as $key => $value) {
						$index = array_search($value['idcardno'],$cardarr);
						// dump($index);
						if ( $index == false && $index !== 0 ){
							array_push($cardarr,$value['idcardno']);
							$value['count'] = 1;
							$value['card'] = $value['idcardno'];

							if ( strlen($value['idcardno']) == 18 ) {
								$search = substr($value['idcardno'], 10,4);
								$strcid = str_replace($search, "****", $value['idcardno']);
								$value['idcardno'] = $strcid;
							}
							array_push($resdata,$value);
						}else{
							$resdata[$index]['count'] = $resdata[$index]['count'] + 1;
						}
					}
				$this -> assign("data1",$resdata);
				if (count($resdata) == 0) {
					$this -> display("nolist");
				}else{
					$this -> display();
				}
			}
		}else{
			$pires = $this -> jubaoview -> where("(tel = $tel or lltel = $tel) and status = 1") -> select();
			$cardarr = array();
				$resdata = array();
				foreach ($pires as $key => $value) {
					$index = array_search($value['idcardno'],$cardarr);
					// dump($index);
					if ( $index == false && $index !== 0 ){
						array_push($cardarr,$value['idcardno']);
						$value['count'] = 1;
						$value['card'] = $value['idcardno'];

						if ( strlen($value['idcardno']) == 18 ) {
							$search = substr($value['idcardno'], 10,4);
							$strcid = str_replace($search, "****", $value['idcardno']);
							$value['idcardno'] = $strcid;
						}
						array_push($resdata,$value);
					}else{
						$resdata[$index]['count'] = $resdata[$index]['count'] + 1;
					}
				}
			$this -> assign("data1",$resdata);
			$coun = count($pires);
			//$this -> assign("count",$coun);
			if ($coun == 0) {
				$this -> display("nolist");
			}else{
				$this -> display();
			}
		}
		
	}


	public function addjb(){
		$data['name'] = $_POST['name'];
		$data['idcardno'] = $_POST['idcardno'];
		$data['tel'] = $_POST['tel'];
		$data['online'] = $_POST['online'];
		$data['area'] = $_POST['area'];
		$data['money'] = $_POST['money'];
		$data['ctime'] = $_POST['ctime'];
		$data['etime'] = $_POST['etime'];
		$data['remark'] = $_POST['remark'];

		$data1['name'] = $_POST['llname'];
		$data1['relation'] = $_POST['llrelation'];
		$data1['tel'] = $_POST['lltel'];
		$data1['address'] = $_POST['lladdress'];
		$data['status'] = 0;
		$data['year'] = date("Y",time());
		$data['datestr'] = date("m-d",time());
		$data['cctime'] = date("Y-m-d",time());

		$data['uid'] = session("userid");
		
		$this -> user ->where(array('id'=>$data['uid']))->setInc('jnum',1);
		
		$pic = $_POST['pic'];

		$res = $this -> jubao -> add( $data );
		if ( $res ) {
			$data1['jid'] = $res;
			$reldata = $this -> relation -> add($data1);
			if ( $pic && $reldata) {
				$arrpic = explode(",",$pic);
				foreach ($arrpic as $key => $value) {
					$picarr = array(
						'pic' => $value,
						'jid' => $res,
						'status' => 0
						);
					$picdata = M("Jpic") -> add( $picarr );
					if (empty($picdata)) {
						apiResponse("error","存储失败！");
					}
				}
				

			}
			apiResponse("success","举报成功！",$res);
		}else{
			apiResponse("error","保存失败！");
		}

	}

	public function chej(){
		$data['id'] = $_POST['id'];
		$data['status'] = 4;
		$res = $this -> jubao -> save( $data );
		if ($res) {
			apiResponse("success","撤销成功！");
		}else{
			apiResponse("error","撤销失败！");
		}
	}

	public function updjinfo(){
		$data['id'] = $_POST['id'];
		$data['name'] = $_POST['name'];
		$data['idcardno'] = $_POST['idcardno'];
		$data['tel'] = $_POST['tel'];
		$data['online'] = $_POST['online'];
		$data['area'] = $_POST['area'];
		$data['money'] = $_POST['money'];
		$data['ctime'] = $_POST['ctime'];
		$data['etime'] = $_POST['etime'];
		$data['remark'] = $_POST['remark'];

		$data1['id'] = $_POST['reid'];
		$data1['name'] = $_POST['llname'];
		$data1['relation'] = $_POST['llrelation'];
		$data1['tel'] = $_POST['lltel'];
		$data1['address'] = $_POST['lladdress'];

		$data['status'] = $_POST['status'];
		$data['year'] = date("Y",time());
		$data['datestr'] = date("d-m",time());
		$data['uid'] = session("userid");

		$pic = $_POST['pic'];

		$res = $this -> jubao -> save( $data );
		$data1['jid'] = $data['id'];
		$reldata = $this -> relation -> save($data1);
	
			$picarr = array(
				'pic' => $pic,
				'jid' => $res,
				'status' => 0
				);
			$picdata = M("Jpic") -> add( $picarr );
		apiResponse("success","举报成功！",$res);
	}


	public function getApi($url){
		$curl = curl_init();
		curl_setopt_array($curl, array(
		  CURLOPT_URL => $url,
		  CURLOPT_RETURNTRANSFER => true,
		  CURLOPT_ENCODING => "",
		  CURLOPT_MAXREDIRS => 10,
		  CURLOPT_TIMEOUT => 30,
		  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		  CURLOPT_CUSTOMREQUEST => "GET",
		  CURLOPT_HTTPHEADER => array(
		    "accept: application/json",
		    "apix-key: 80fcec4bc007472f7a79e4d59f838dcd",
		    "content-type: application/json"
		  ),
		));

		$response = curl_exec($curl);
		$err = curl_error($curl);

		curl_close($curl);

		if ($err) {
		  return "cURL Error #:" . $err;
		} else {
		  return $response;
		}
	}

	public function uploadPic(){
        $pic       = $_POST['pic'];
        $pic_name      = $_POST['pic_name'];
        $temp = explode('.',$pic_name);
        $ext = uniqid().'.'.end($temp);
        $base64    = substr(strstr($pic, ","), 1);
        $image_res = base64_decode($base64);
        $pic_link  = "Uploads/jubao/".$ext;
        $saveRoot = "Uploads/jubao/";
        //检查目录是否存在  循环创建目录
        if(!is_dir($saveRoot)){
            mkdir($saveRoot, 0777, true);
        }
        $res = file_put_contents($pic_link ,$image_res);
        if($res){
            $ajaxData = array("flag" => "success", "message"=>"上传成功！" );
            $ajaxData['data'] = $pic_link;
            echo json_encode($ajaxData);
        }else{
            $ajaxData = array("flag" => "error", "message"=>"上传失败","data" => array());
            echo json_encode($ajaxData);
        }
    }
}